// pages/my/all-orders/all-orders.jsc
const request = require('../../../../../utils/request')
Page({

    /**
     * 页面的初始数据
     */
    data: {


        //选项卡
        type: 2,
        typeIndex: 0,
        typeList: [{
            name: '配送',
            type: 2
        },
        {
            name: '快递',
            type: 1
        }
        ],
        page: 1,
        limit: 10,
        navlist: [{
            name: '全部',
            status: 'all',
            index: 0
        },
        {
            name: '待支付',
            status: 'payment',
            index: 1
        },
        {
            name: '待出库',
            status: 'chuku',
            index: 2
        },
        {
            name: '待配送',
            status: 'undelivered',
            index: 3
        },
        {
            name: '已取消',
            status: 'cancel',
            index: 4
        },
        {
            name:'退款/售后',
            status:'aftersaleed',
            index:5
        }

        ],
        //子导航选项卡-配送
        currentTab: 0,
        status: 'all', //订单状态
        list: [],
        isShowReason: false,
        explain: '',
        selectOrder: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // if (options.status && options.status != 'undefined') {
        //     this.setData({
        //         currentTab: this.data.navlist.find(el => el.status == options.status).index,
        //         status: options.status
        //     })
        // } else {
        //     this.setData({
        //         currentTab: 0,
        //         status: 'all'
        //     })
        // }

    },
    onShow() {
        this.setData({
            list: [],
            page: 1
        })
        this.getList()
    },
    /**
     * 选项卡
     */
    // typeClick(e) {
    //     const index = e.currentTarget.dataset.index;
    //     const item = e.currentTarget.dataset.item;
    //     this.setData({
    //         typeIndex: index,
    //         type: item.type,
    //         page: 1,
    //         isLastPage: false,
    //         list: []
    //     })
    //     this.getList()
    // },
    /**
     * 支付收发货状态
     */
    // tabClick(e) {
    //     const index = e.currentTarget.dataset.index;
    //     let item = e.currentTarget.dataset.item;
    //     this.setData({
    //         currentTab: index,
    //         status: item.status,
    //         page: 1,
    //         list: [],
    //         isLastPage: false,
    //     })
    //     this.getList()
    // },
    // 详情
    onDetails(e) {
        let id = e.currentTarget.dataset.id
        let item = e.currentTarget.dataset.item
        if (item.order_type == 1) {
            //普通订单
            wx.navigateTo({
                url: '/pages/mine/pages/my/order-details/order-details?id=' + id,
            })
        } else {
            // 拼团订单
            wx.navigateTo({
                url: '/pages/home/pages/my/group-details/group-details?id=' + id,
            })
        }
    },
    getList() {
        let _this = this
        request._post('/api?method=order.getorderlist', {
            status:'aftersale',
            page: this.data.page,
            limit: this.data.limit,
        }).then(res => {
            if (res.data.list.length == 0 || res.data.list == null) {
                _this.setData({
                    isLastPage: true,
                })
            }
            _this.setData({
                list: _this.data.list.concat(res.data.list)
            })
        })
    },
    // 上拉加载
    onReachBottom: function () {
        if (this.data.isLastPage) {
            return
        }
        let page = this.data.page
        page++
        this.setData({
            page,
        })
        this.getList()
    },
    // 操作订单
    handleOrder(e) {
        let type = e.currentTarget.dataset.type
        let item = e.currentTarget.dataset.item
        this.setData({
            selectOrder: item,
        })
        if (type == 1) { //取消订单
            this.setData({
                isShowReason: true,
            })
        } else if (type == 2) { //删除订单
            this.delOrderApi()
        } else if (type == 3) { //提醒发货
            this.remindSend()
        } else if (type == 4) { //延长收货
            this.lengthenOrder()
        } else if (type == 5) { //再次购买
            this.reBuy()
        } else if (type == 6) { //确认收货 申请售后
            // this.sureGetOrder()
            this.applyShouhou()
        }
        
    },
    // 关闭弹框
    closeReason() {
        this.setData({
            isShowReason: false
        })
    },
    // 提交取消订单
    submit(e) {
        this.setData({
            explain: e.detail,
            isShowReason: false
        })
        this.cancelOrderApi()
    },
    // 取消订单api
    cancelOrderApi() {
        let _this = this
        let selectOrder = this.data.selectOrder
        request._get('/api?method=order.cancel', {
            order_id: selectOrder.order_id,
            explain: this.data.explain
        }).then(res => {
            getApp().toast(res.msg)
            _this.setData({
                list: [],
                page: 1,
                isLastPage: false,
            })
            setTimeout(function () {
                _this.getList()
            }, 1000)
        })
    },
    // 删除订单
    delOrderApi() {
        let _this = this
        wx.showModal({
            title: '提示',
            content: '确认删除当前订单？',
            success(res) {
                if (res.confirm) {
                    request._get('/api?method=order.del', {
                        order_id: _this.data.selectOrder.order_id
                    }).then(res => {
                        getApp().toast(res.msg)
                        _this.setData({
                            list: [],
                            isLastPage: false,
                            page: 1
                        })
                        setTimeout(function () {
                            _this.getList()
                        }, 1000)
                    })
                } else if (res.cancel) { }
            }
        })
    },
    // 申请售后
    applyShouhou() {
      let _this = this
      wx.showModal({
          title: '提示',
          content: '是否申请售后？',
          success(res) {
              if (res.confirm) {
                  request._post('/api?method=order.addAftersale', {
                      order_id: _this.data.selectOrder.order_id
                  }).then(res => {
                      getApp().toast(res.msg)
                      _this.setData({
                          list: [],
                          isLastPage: false,
                          page: 1
                      })
                      setTimeout(function () {
                          _this.getList()
                      }, 1000)
                  })
              } else if (res.cancel) { }
          }
      })
    },
})